<?php
namespace app\serversetting\logic;

use \app\manage\logic\BaseLogic;
use \think\Db;

/**
 * @author:      李颖
 * @DateTime:    2017-06-21 14:04:12
 * @Description: 全局设置
 */
class Setting extends BaseLogic
{
	protected $name = 'setting';
    protected $config = 'db_config_setting';
    protected $settingCategoryLogic = 'app\serversetting\logic\SettingCategory';
    protected $settingModel = 'app\serversetting\model\Setting';
    protected $settingCategoryModel = 'app\serversetting\model\SettingCategory';
    protected $addAllowField = [];
    protected $searchWhere = [];

    use \app\serveruser\traits\addAppId;
    use \app\serversetting\traits\OperateModel;

    /*内部获取某个配置的详情*/
    public function dbInfo($data) 
    {
        return $this->db()->where(['id'=>$data['id'],'status'=>1])->find();
    }

    /*添加一个设置*/
    public function addSet($data): array 
    {
        unset($data['app_id']);
    	$res = $this->model($this->settingModel)->save($data);
    	return auto_return($res);
    }

    /*后台编辑一个正常的配置*/
    public function editSet($data): array 
    {
        return $this->_editOne($data, 'setting', 'setting_category', $this->db());
    }

    /*后台获取某个不被删除的配置*/
    public function getOneSet($data): array 
    {
        $res = $this->_getOne($data, 'setting', 'setting_category', $this->model($this->settingModel));
        if($res['status'] != SUCCESS_STATUS){
            return $res;
        }
        $res['res']['cate_ids'] = logic('\app\serversetting\logic\SettingCategory')->getParentIds($res['res']['category_id']);
        return $res;
    }

    /*后台获取某个应用的不被删除的配置列表*/
    public function getAppSet($data): array 
    {
        return $this->_getList($data, 'setting', 'setting_category', $this->model($this->settingModel));
    }

    /*检验识别码的唯一性*/
    public function checkAction($data): array 
    {
        $info = $this->db()->where(['action'=>$data['action']])->find();
        if($info){
            return auto_failure(null, '该识别码action已存在');
        }else{
            return auto_success(null, '识别码有效');
        }
    }

    /*后台获取某些应用下的没被删除的配置列表 分类中的action*/
    public function getCateSetList($data): array 
    {
        $res = $this->_getList(['app_id'=>$data['app_id'],'category_action'=>$data['action']], 'setting', 'setting_category', $this->model($this->settingModel), false);

        $returnRes = [];
        foreach ($res['res'] as $k => $v) {
            $returnRes[$v['action']] = $v['value'];
        }

        return auto_return($returnRes);
    }

    /*根据action获取多个配置详情 配置表中的action*/
    public function getSomeSet($data): array 
    {
        return $this->_getList($data, 'setting', 'setting_category', $this->model($this->settingModel), false);
    }


}